package com.ita.controller;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.socket.TextMessage;

import com.ita.dto.AssessDto;
import com.ita.pojo.Assess;
import com.ita.service.AssessService;
import com.ita.service.OrderService;
import com.ita.util.Constant;
import com.ita.util.JsonResult;

@RestController
@RequestMapping("api/assess")
@CrossOrigin
public class APIAssessController {

	@Autowired(required = true)
	private AssessService aService;
	
	
	@Resource(name = "orderService")
	private OrderService oService;

	@RequestMapping(value = "/add", method = RequestMethod.PUT, consumes = "application/json")
	public JsonResult addAssess(@RequestBody AssessDto assessDto) {

		JsonResult result = new JsonResult();
		Assess assess = null;
		try {
			assess = aService.addClientAssess(assessDto);
			oService.updateState(assessDto.getOid(), Constant.ORDER_ASSESS);
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(Constant.FAIL);
		}
		if (assess == null)
			result.setCode(Constant.FAIL);
		else
			result.setCode(Constant.SUCCESS);
		TextMessage message = new TextMessage(Constant.ASSESSCOMING);
		MyWsHandler.sendMessage(assessDto.getMid(), message);

		return result;
	}

}
